<?php
exit('程序不能被执行！！！！');
set_time_limit(0);

//创建文件夹
function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;

    if (!mkdirs(dirname($dir), $mode)) return FALSE;

    return @mkdir($dir, $mode);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php 获取样式及图片</title>
<meta name="keywords" content="php 获取样式及图片" />
<meta name="description" content="php 获取样式及图片" />
</head>
<body>
<div style="margin-left:20px;margin-top:20px">
	<?php
		if(@$_POST['url']){
			include 'simplehtmldom_1_5/simple_html_dom.php';
			
			$url = 'http://www.36kr.com/';
			
			$html_dom = file_get_html($_POST['url']);
			
			$links = $html_dom->find('link');
			
			$link_arr = array();
			echo '匹配到的css地址：<br>';
			foreach($links as $element){
				echo $element->href.'<br>';
				if(!empty($element->href) && strpos($element->href, '.css')){
					
					if(strpos($element->href, 'http://') && strpos($element->href, '36kr.com')){
						$link_arr[] = $element->href;
					}else{
						$link_arr[] = 'http://www.36kr.com' . $element->href;
					}
				}
			}
			echo '可用css地址：<br>';
			foreach((array)$link_arr as $v){
				$newfile = explode('?',basename($v));
				$newfile = $newfile[0];
				
				$dir_css = dirname(__FILE__).'/css/';
				mkdirs($dir_css);
				
				if (!copy($v, './css/'.$newfile)) {
					echo '<font color="red">失败</font>' . $v . '<br>';
				}else{
					echo '<font color="blue">成功</font>' . $v . '<br>';
					
					$data = file_get_contents($v);
					preg_match('/(.*\/\/.*?)\//', $url, $host);

					$host = $host[1];

					$regex = '/url\(\'{0,1}\"{0,1}(.*?)\'{0,1}\"{0,1}\)/';

					preg_match_all($regex, $data, $result);

					$ret = array_unique($result[1]);
					foreach ($ret as $val) {
						$check_type = pathinfo($val);

						if($check_type['extension']&& in_array($check_type['extension'],array('gif','jpg','png','bmp','jpeg'))){
							//样式图片中含有../的路径
							$real_val = str_replace('..','',$val);
							if (preg_match('/^http.*/', $real_val)) {
								$target = $real_val;
							} else if (preg_match('/^\/.*/', $real_val)) {
								$target = $host . $real_val;
							} else {
								$target = $url . $real_val;
							}
							$dir_info = parse_url($target);
							
							$dir = dirname(__FILE__).'/images/'.dirname($dir_info['path']);
							mkdirs($dir);

							preg_match('/.*\/(.*\.\D+)$/', $real_val, $name);
				
							if (!is_file($dir . '/' . $name[1])) {
								if(!copy($target, $dir . '/' . $name[1])){
									echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">失败</font>' . $target . '<br>';
								}else{
									echo '&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">成功</font>' . $target . '<br>';
								}
							}
						}else{
							echo '&nbsp;&nbsp;&nbsp;&nbsp;不是图片文件'.$v.'<br>';
						}
					}
				}

			}
			
			$html_dom->clear();
			
			echo '<br><a href ="http://127.0.0.1/test/">返回</a><br><br>';
		}
	?>
	<form action="http://127.0.0.1/test/" method="post">
	页面地址：<input type="text" id="url" value="" name="url" size="70">
	<input type="submit" id="sub" value="  获  取  " name="sub">
	</form>
</div>
</body>
</html>